package com.baizhi.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.baizhi.entity.Admin;
import com.baizhi.service.AdminService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @RequestMapping("login")
    public String login(String name, String password, String validate, HttpSession session){
//        System.err.println(validate);
//        try {
//            Admin admin = adminService.selectOne(name, password, validate, session);
//            if (admin == null){
//                return "redirect:/main.jsp";
//            }
//            session.setAttribute("login",admin);
//            return "redirect:/main.jsp";
//        } catch (Exception e) {
//            e.printStackTrace();
//            return "redirect:/login.jsp";
//        }
        UsernamePasswordToken token = new UsernamePasswordToken(name,password);
        Subject subject = SecurityUtils.getSubject();
        try {
            String code = (String) session.getAttribute("code");
            if (!code.equals(validate)){
                throw new RuntimeException("验证码错误");
            }
            subject.login(token);
            return "redirect:/main.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            return "redirect:/login.jsp";
        }


    }
    @RequestMapping("selectAll")
    @ResponseBody
    public Map selectAll(Integer page,Integer limit){
        Map map = new HashMap();
        PageInfo<Admin> info = adminService.selectAll(page, limit);
        map.put("code",0);
        map.put("msg","");
        map.put("count",info.getTotal());
        map.put("data",info.getList());
        return map;
    }
    @RequestMapping("insert")
    @ResponseBody
    public Map insert(Admin a){
        Map map = new HashMap();
        try{
            adminService.insert(a);
            map.put("flag",true);
        }catch (Exception e){
            e.printStackTrace();
            map.put("flag",false);
        }
        return map;
    }
    @RequestMapping("getCode")
    public void getCode( HttpServletResponse response) throws IOException {
        Session session = SecurityUtils.getSubject().getSession();
        LineCaptcha captcha = CaptchaUtil.createLineCaptcha(200, 100);
        System.err.println(captcha.getCode());
        session.setAttribute("code",captcha.getCode());
        captcha.write(response.getOutputStream());
    }
}
